package dao

import (
	"PrintTrnsferServer/common"
	"fmt"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Manager interface {
	FindOrder(string) *Order
}

type manager struct {
	db *gorm.DB
}

type Order struct {
	Id       string `sql:"id"`
	CreateBy string `sql:"create_by"`
}

var DbManager Manager

func init() {
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local", common.Config.Db.Username, common.Config.Db.Password, common.Config.Db.Hostname, common.Config.Db.Hostport, common.Config.Db.Database)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal("Failed to init db:", err)
	}
	DbManager = &manager{db: db}
}

func (mgr *manager) FindOrder(param string) (result *Order) {
	mgr.db.Raw("SELECT id, create_by FROM oms_order WHERE id = ?", param).Scan(&result)
	return
}
